#include "sort.h"
#include "stack.h"


void InsertSort(int* a, int n)
{
	// 升 序 排 序
	/*for (int i = 1; i <= n - 1; i++) {
		int j = i;
		while (a[j] < a[j - 1]&&j>0) {
			int tmp = a[j];
			a[j] = a[j - 1];
			a[j - 1] = tmp;
			j--;
		}
	}*///交换思路// 

	//下标(指针)思路
	

	for (int i = 1; i < n; i += 1) {
			//单趟[0,end]有序插入a[end+1]
		int end = i;
		int tmp = a[end];
		while (tmp<a[end-1]&&end>=1) {
			a[end] = a[end - 1];
			end--;
		}
		a[end] = tmp;
	}
}
void ShellSort(int* a, int n) {
	int gap = n;
	while (gap>1) {
		//gap>1时进行的是预排序
		gap = gap / 4 + 1;//最后一次排序是接近有序的插入排序
		for (int i = 0; i < n-gap; i++) {
			//单趟[0,end]有序插入a[end+1]
			int end = i+gap;
			int tmp = a[end];
			while ( end-gap>=0&&tmp > a[end - gap] ) {
				a[end] = a[end - gap];
				end-=gap;
			}
			a[end] = tmp;
		}	
	}	
}
void Swap(int* p1, int* p2) {
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void SelectSort(int* a, int n) {
		int begin = 0;
		int end = n - 1;
	while (begin < end) {
		int max = begin;
		int min = begin;
		for (int i = begin; i <= end ; i++)
		{	
			if (a[max] < a[i])max = i;
			if (a[min] > a[i])min = i;	
		}
		if (min == end)min = max;
		Swap(&a[max], &a[end]);
		Swap(&a[min], &a[begin]);

		end--;
		begin++;
	}
}
//向下调整
void AdjustDown(int* a, int size, int parent) {
	int child = parent * 2 + 1;
	while (child < size) {
		if (child + 1 < size && a[child + 1] > a[child])child++;
		if (a[child] > a[parent]) {
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
			break;
	}
}
void HeapSort(int* a, int n) {
	//建堆,升序排，建大堆
	//向下调整O(N)
	int parent = (n - 1 - 1) / 2;
	for (int i = parent; i >= 0; i--) {
		AdjustDown(a, n, i);
	}
	//排序O(N*logN)
	for (int i = 0; i < n-1; i++) {
		Swap(&a[0], &a[n - 1 - i]);
		AdjustDown(a, n - 1 - i, 0);
	}
}
void BubbleSort(int* a, int n) {
	for (int j = 0; j < n - 1; j++) {
		for (int i = 0; i < n-1-j; i++)
			if (a[i] > a[i + 1])Swap(&a[i], &a[i + 1]);
	}
}
//》》》》》》》》》》》》》快速排序《《《《《《《《《《《《《《
// 
// 三 数 取 中 优 化
void GetMid0(int* a, int begin, int end) {
	int left = a[begin];
	int mid = a[(begin + end) / 2];
	int right = a[end];
	if (left < mid && left < right) {
		if (mid < right) {
			a[begin] = mid;
			a[(begin + end) / 2] = left;
		}
		else {
			a[begin] = right;
			a[end] = left;
		}
	}
	else if (left > mid && left > right) {
		if (mid > right) {
			a[begin] = mid;
			a[(begin + end) / 2] = left;
		}
		else {
			a[begin] = right;
			a[end] = left;
		}
	}
}
int GetMid(int* a, int begin, int end) {
	int mid = (begin + end) / 2;
	if (a[mid] > a[begin]) {
		if (a[mid] < a[end])return mid;
		if (a[begin] > a[end])return begin;
		return end;
	}
	else {
		if (a[mid] > a[end])return mid;
		if (a[end] > a[begin])return end;
		return begin;
	}
}
//------------hoare 版 本----------------------
int QuickSort1(int* a, int begin, int end) {
	//优化一：三数取中
	//GetMid0(a, begin, end);
	int mid = GetMid(a, begin, end);
	Swap(&a[mid], &a[begin]);

	int keyi = begin;
	int left = begin;
	int right = end;
	while (left < right) {
		while (left<right && a[right]>=a[keyi])--right;
		while (left<right && a[left]<=a[keyi])++left;
		if(left!=right)
		Swap(&a[left], &a[right]);
	}
	Swap(&a[keyi], &a[left]);
	keyi = left;
	return keyi;
}
//------------挖  坑  法----------------------
int QuickSort2(int* a, int begin, int end) {
	//优化一：三数取中
	int mid = GetMid(a, begin, end);
	Swap(&a[mid], &a[begin]);

	int piti = begin;//第一个坑位
	int pit = a[begin];//储存变量
	int left = begin;
	int right = end;
	while (left < right) {
		while (left < right && a[right] >= a[piti])right--;
		a[piti] = a[right];
		piti = right;

		while (left < right && a[left] <= a[piti])left++;
		a[piti] = a[left];
		piti = left;
	}
	a[piti] = pit;
	return piti;
}
//-------------------前 后 指 针 法----------------------
int QuickSort3(int* a, int begin, int end) {
	//优化一：三数取中
	int mid = GetMid(a, begin, end);
	Swap(&a[mid], &a[begin]);

	int keyi = begin;
	int prev = begin;
	int cur = prev + 1;
	while (cur <= end) {
		if (a[cur] < a[keyi] && ++prev != cur)
			Swap(&a[prev], &a[cur]);
		++cur;
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;
	return keyi;
}
void QuickSort(int* a, int begin, int end) {
	if (begin >= end)return;

	//优化二：小区间优化
	if (end - begin > 20) {
		int keyi = QuickSort1(a, begin, end);
		if (keyi > 0) {
			QuickSort(a, begin, keyi - 1);
			QuickSort(a, keyi + 1, end);
		}
		
	}
	else {
		InsertSort(a + begin, end - begin + 1);
		//HeapSort(a + begin, end - begin + 1);
	}
}
//快排的非递归实现（借助栈）
void QuickSortNonR(int* a, int left, int right) {
	Stack s;
	StackInit(&s);
	StackPush(&s, left);
	StackPush(&s, right);
	while (!StackEmpty(&s)) {
		int r = StackTop(&s);
		StackPop(&s);
		int l = StackTop(&s);
		StackPop(&s);
		//小区间优化
		if (r-l<= 10) {
			InsertSort(a + l, r-l + 1);
		}
		else {
			int keyi = QuickSort1(a, l, r);
			if (l < keyi - 1) {
			StackPush(&s, l);
			StackPush(&s, keyi - 1);
			}
			if (r > keyi+1) {
			StackPush(&s, keyi+1);
			StackPush(&s, r);
			}

		}
		
	}
	StackDestroy(&s);
}